package operator;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;
import java.util.Arrays;
import java.util.List;

/**
 * Created by huangxiaoli on 2019/11/27 13:53
 */
public class sortByKey {

    public static void main(String[] args) {
        SparkConf conf = new SparkConf()
                .setAppName("sortByKey")
                .setMaster("local");

        JavaSparkContext sc = new JavaSparkContext(conf);

        List<Tuple2<Integer, String>> scoreList = Arrays.asList(
                new Tuple2<Integer, String>(78, "marry"),
                new Tuple2<Integer, String>(89, "tom"),
                new Tuple2<Integer, String>(72, "jack"),
                new Tuple2<Integer, String>(86, "leo"));

        JavaPairRDD<Integer, String> scores = sc.parallelizePairs(scoreList);

        JavaPairRDD<Integer, String> sortedScores = scores.sortByKey();
        sortedScores.foreach(new VoidFunction<Tuple2<Integer,String>>() {
            @Override
            public void call(Tuple2<Integer, String> t) throws Exception {
                System.out.println(t._1 + ":" + t._2);
            }
        });
        sc.close();
    }

}
